var domain ='{$setting.api_host}'
var api=getapi(domain)
	console.log(api)
	$("#loginsumbit").on('click', function() {
		console.log(11)
		
		if($('input[name="username"]').val()){
			if($('input[name="password"]').val()){
				ajax(api.login.in,{
					username:$('input[name="username"]').val(),
					password:$('input[name="password"]').val()
				},'post').done(function(res) {
						console.log(res)
						if(res.code==1){
							var nowdate=new Date();
							nowdate.setTime(nowdate.getTime()+30*60*1000)
							console.log(nowdate)
							 $.cookie("token",res.data["user-token"],{path:'/'});
							 $.cookie("user",JSON.stringify(res.data["user"]),{path:'/'});
							Msg('登陆成功');
							console.log($.cookie("token"))
							console.log($.cookie("lastpath"))
							if($.cookie("lastpath")){
								window.location.href=$.cookie("lastpath")
							}else{
								window.location.href="{:url('/html/car/car_statest')}"
							}
						}else{
							Err(res.msg)
						}
				}).fail(function(err) {
					Err('请求失败')
				});
			}else{
				Err('密码不能为空')
			}
		}else{
			Err('用户名不能为空')
		}
		
	
	});
	　$(document).keydown(function(event){
		if(event.keyCode == 13){
			if($('input[name="username"]').val()){
			if($('input[name="password"]').val()){
				ajax(api.login.in,{
					username:$('input[name="username"]').val(),
					password:$('input[name="password"]').val()
				},'post').done(function(res) {
						console.log(res)
						if(res.code==1){
							var nowdate=new Date();
							nowdate.setTime(nowdate.getTime()+30*60*1000)
							$.cookie("token",res.data["user-token"],{path:'/'});
							 $.cookie("user",JSON.stringify(res.data["user"]),{path:'/'});
							Msg('登陆成功');
							console.log($.cookie("lastpath"))
							if($.cookie("lastpath")){
								window.location.href=$.cookie("lastpath")
							}else{
								window.location.href="{:url('/html/car/car_statest')}"
							}
							
						}else{
							Err(res.msg)
						}
				}).fail(function(err) {
					Err('请求失败')
				});
			}else{
				Err('密码不能为空')
			}
		}else{
			Err('用户名不能为空')
		}
　　　　}
	});
	$('.send_message').on('click', function() {
		console.log($('.phone').val())
		var that=this 
		if($(this).data('click')) {
			var load = layer.load(0, {shade: false});
			var reg = /^1[345789]\d{9}$/g;
			var phoneformat =reg.test($('.phone').val());
			console.log(phoneformat)
			
			if(phoneformat) {
				console.log($('.phone').val())
				$.ajax({
					type: "post",
					url: api.login.Sms,
					data: {
						mobile: $('.phone').val()
					},
					success: function(res) {
						layer.close(load);
						console.log(res)
						if(res.code == 1) {
							Msg('发送成功');
							$(that).removeClass('am-btn-primary');
							$(that).addClass('am-btn-default');
							$(that).html(60);
							$(that).prop('disabled', true)
							$(that).data('click', false);
							
							var idx = setInterval(function() {
								var time = $(that).html() - 0;
								time -= 1;
								$(that).html(time);
								if($(that).html() == 0) {
									$(that).data('click', true);
									clearInterval(idx);
									$(that).prop('disabled', false)
									$(that).removeClass('am-btn-default');
									$(that).addClass('am-btn-primary');
									$(that).html('重新发送');
								}
							}, 1000);
						} else {
							Err(res.msg);
						}
					}
				})
			} else {
				layer.close(load);
				Err('手机格式不正确');
			}
		} else {
		}
	})
	$('.phone_login_sumbit').on('click', function() {
		if($(this).closest('form').find('input[name="sms_code"]').val().length == 6) {
			var reg = /^1[345789]\d{9}$/g;
			var phoneformat =reg.test($('.phone').val());
			console.log(phoneformat)
			if(phoneformat){
				ajax(api.login.smsLogin,{
					mobile:$('.phone').val(),
					code:$('input[name="sms_code"]').val()
				},'post').done(function(res) {
						console.log(res)
						if(res.code==1){
							$.cookie("token",res.data["user-token"],{expires:res.data.expires/3600});
							Msg('登陆成功');
							console.log($.cookie("token"))
							window.location.href="{:url('/html/index/index')}"
						}else{
							Err(res.msg)
						}
				}).fail(function(err) {
					Err('请求失败')
				});
			}else{
				Err('手机格式不正确');
			}
		} else {
			Err('手机验证码有误');
		}
	})


function goTo(url, newopen) {
	if(url == -1) {
		history.back();
	} else if(url == 0) {
		return false
	} else if(url == 1) {
		window.location.reload();
	} else if(url) {
		if(newopen) {
			window.open(url);
		} else {
			window.location.href = url;
		}

	}
}

function rslt(res, mode, formId) {
	var newopen = 0;
	var mode = 1;
	//	Load(1);

	if(res.code) {
		rmsg = res.msg ? res.msg : '操作成功！';
		url = res.url ? res.url : res.data.url;
		if(mode) {
			Msg(rmsg);
			goTo(url);
		} else {}

	} else {
		Err(res.msg ? res.msg : '操作失败，稍候再试');
		formId.removeClass('disabled').data('txt', formId.text()).html("提交").removeAttr('disabled');
	}
}

function goTo(url, newopen) {
	if(url == -1) {
		history.back();
	} else if(url == 0) {
		return false
	} else if(url == 1) {
		window.location.reload();
	} else if(url) {
		if(newopen) {
			window.open(url);
		} else {
			window.location.href = url;
		}

	}
}

function Msg(msg, func) {
	layer.msg(msg, {
		icon: 1
	}, func);
}

function Err(msg) {
	layer.msg(msg, {
		icon: 2
	});
}

function Load(close) {
	if(close) {
		layer.closeAll('loading');
	} else {
		layer.load(0, {
			shade: false,
		});
	}
}

function chang(a) {
	if(a.value != 0) {

		$(a).parent().next().html("")
	} else {

	}
}